package chkui.springcore.example.hybrid.beanmanipulation;

import java.beans.IntrospectionException;
import java.text.DateFormat;
import java.util.Date;
import java.util.Properties;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import chkui.springcore.example.common._G;
import chkui.springcore.example.hybrid.beanmanipulation.bean.Address;
import chkui.springcore.example.hybrid.beanmanipulation.bean.Person;
import chkui.springcore.example.hybrid.beanmanipulation.bean.Vehicle;
import chkui.springcore.example.hybrid.beanmanipulation.propertyEditor.AddressEditor;

public class BeanManipulationApp {

	public static void main(String[] args) throws IntrospectionException {
		new BeanManipulationApp().simpleDataBind().nestedDataBind().propertyEditor().iocWithPropertyEditor()
				.javaBeanStandardPropertyEditor();
	}

	private BeanManipulationApp simpleDataBind() {
		// 简单数据转换
		BeanWrapper wrapper = new BeanWrapperImpl(new Person());
		
		//使用 BeanWrapper::setPropertyValue 接口设置数据
		wrapper.setPropertyValue("name", "niubility");	
		wrapper.setPropertyValue("age", 18);
		wrapper.setPropertyValue("license", true);
		_G.print(wrapper.getWrappedInstance());

		//使用 Properties对象设置数据，Properties实例可以来源于*.properties文件
		Properties p = new Properties();
		p.setProperty("name", "From Properties");
		p.setProperty("age", "25");
		p.setProperty("license", "false");
		p.setProperty("otherInfo[birthday]", "2000-01-01");
		wrapper.setPropertyValues(p);
		_G.print(wrapper.getWrappedInstance());

		return this;
	}

	private BeanManipulationApp nestedDataBind() {
		// 数据嵌套转换
		BeanWrapper wrapper = new BeanWrapperImpl(new Vehicle(new Person()));

		Properties p = new Properties();
		p.setProperty("name", "Envision");
		p.setProperty("manufacturer", "Buick");
		
		//person.name表示设置person域的name变量数值
		p.setProperty("person.name", "Alice");
		p.setProperty("person.age", "25");
		p.setProperty("person.license", "true");
		p.setProperty("person.otherInfo[license code]", "123456789");
		wrapper.setPropertyValues(p);
		_G.print(wrapper.getWrappedInstance());

		return this;
	}

	private BeanManipulationApp propertyEditor() {
		
		//使用预设转换工具和自定义转换工具
		BeanWrapper wrapper = new BeanWrapperImpl(new Person()); // 新建包装

		// 设定日期转换格式
		DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd");
		
		// 将Editor与DateFormat进行帮顶，使用指定的格式
		CustomDateEditor dateEditor = new CustomDateEditor(df, false);
		
		// 创建AddressEditor实例
		AddressEditor addressEditor = new AddressEditor();
		
		// 注册dateEditor，将其与Date类进行绑定
		wrapper.registerCustomEditor(Date.class, dateEditor);
		
		// 注册addressEditor，将其与Address类进行绑定
		wrapper.registerCustomEditor(Address.class, addressEditor);

		wrapper.setPropertyValue("name", "Bob");
		// CustomNumberEditor执行转换
		wrapper.setPropertyValue("age", "18");
		// CustomBooleanEditor执行转换
		wrapper.setPropertyValue("license", "false");
		// dateEditor执行转换
		wrapper.setPropertyValue("birtday", "1999-01-30");
		
		wrapper.setPropertyValue("address", "广东-广州-白云");
		_G.print(wrapper.getWrappedInstance());
		return this;
	}

	private BeanManipulationApp iocWithPropertyEditor() {

		// IoC容器添加Bean转换数据
		ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(BeanManipulationConfig.class);
		_G.print(ctx.getBean(Person.class));
		_G.print(ctx.getBean(Vehicle.class));
		ctx.close();
		return this;
	}

	private BeanManipulationApp javaBeanStandardPropertyEditor() throws IntrospectionException {
		
		//JavaBean自动帮顶转换
		BeanWrapper wrapper = new BeanWrapperImpl(Vehicle.class);
		wrapper.setPropertyValue("tyre", "215/70R 15");
		_G.print(wrapper.getWrappedInstance());
		return this;
	}
}
